package cn.edu.scau.cmi.ema.service.pagination;


import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.dao.FoodmaterialDAO;
import cn.edu.scau.cmi.ema.domain.Foodmaterial;
import cn.edu.scau.cmi.ema.service.FoodmaterialService;
import cn.edu.scau.cmi.ema.service.base.CmiPaginationService;
import cn.edu.scau.cmi.ema.util.CmiEntityUtil;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

@Service("FoodmaterialPaginationService")
public class FoodmaterialPaginationService extends CmiPaginationService {
	@Autowired
	private FoodmaterialDAO foodmaterialDAO;

	@Autowired
	private FoodmaterialService foodmaterialService;

	public Set<Foodmaterial> getPaginationFoodmaterials(CmiPagination page) {
		String sql = CmiSqlUtil.createSelectHql(page, Foodmaterial.class);
		Set<Foodmaterial> foodmaterials = foodmaterialDAO.findFoodmaterialByCompositeProperities(sql,
				(page.getCurrentPagination() - 1) * page.getPaginationSize(), page.getPaginationSize());
		return foodmaterials;
	}

	// 可以考虑直接到父类型中，使用反射的方式。
	public CmiPagination updatePagination(HttpServletRequest request, CmiPagination cmiPagination) {
		// （1）获取Foodmaterial所有基本属性的名称
		Set<String> basicPropertyNames = CmiEntityUtil.getAllBasicFieldNames(Foodmaterial.class);
		// （2）获取Foodmaterial所有基本属性和类型的Map
		cmiPagination.setPropertyTypeMap(CmiEntityUtil.getAllFieldNameTypeMap(Foodmaterial.class));
		// （3）获取所有页面传过来的基本属性的查找条件
		Map<String, String> basicPropertyCriteriaMap = cmiPagination.getBasicPropertyCriteriaMap();
		// （4）如果页面传递过来的参数是有效的输入，将有效的基本属性的输入添加到basicPropertyCriteriaMap中
		for (String basicPropertyName : basicPropertyNames) {
			String basicPropertyValue = request.getParameter(basicPropertyName);
			if (basicPropertyValue != null && !"".equalsIgnoreCase(basicPropertyValue.trim())) {// ??????
				basicPropertyCriteriaMap.put(basicPropertyName, basicPropertyValue);
			} else {
				basicPropertyCriteriaMap.put(basicPropertyName, "");
			}
		}
		// （5）查找符合分页条件的所有记录的数量
		Integer totalRecords = foodmaterialService.countFoodmaterials(cmiPagination);
		// （6）调用父类的updatePagination更新方法更新通用的基本属性
		cmiPagination = super.updatePagination(cmiPagination, totalRecords);
		return cmiPagination;
	}

	// 可以考虑直接到父类型中，使用反射的方式。
	public CmiPagination updateAppPagination(CmiPagination cmiPagination) {

		// （1）获取Foodmaterial所有基本属性的名称
		Set<String> basicPropertyNames = CmiEntityUtil.getAllBasicFieldNames(Foodmaterial.class);
		// （2）获取Foodmaterial所有基本属性和类型的Map
		cmiPagination.setPropertyTypeMap(CmiEntityUtil.getAllFieldNameTypeMap(Foodmaterial.class));
		// （3）获取所有页面传过来的基本属性的查找条件
		Map<String, String> basicPropertyCriteriaMap = cmiPagination.getBasicPropertyCriteriaMap();
		// （4）如果页面传递过来的参数是有效的输入，将有效的基本属性的输入添加到basicPropertyCriteriaMap中
//		for (String basicPropertyName : basicPropertyNames) {
//			String basicPropertyValue = request.getParameter(basicPropertyName);
//			if (basicPropertyValue != null && !"".equalsIgnoreCase(basicPropertyValue.trim())) {// ??????
//				basicPropertyCriteriaMap.put(basicPropertyName, basicPropertyValue);
//			} else {
//				basicPropertyCriteriaMap.put(basicPropertyName, "");
//			}
//		}
		// （5）查找符合分页条件的所有记录的数量
		Integer totalRecords = foodmaterialService.countFoodmaterials(cmiPagination);
		// （6）调用父类的updatePagination更新方法更新通用的基本属性
		cmiPagination = super.updatePagination(cmiPagination, totalRecords);
		return cmiPagination;
	}
}

